layui.define(['form','table'], function(exports){
  var $ = layui.$,form = layui.form,table = layui.table;

  var userId = $('input[name="userId"]').val();
  var relationUserId = $('input[name="relationUserId"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/agent/relation/user/func_model/list.data'
      ,where:{userId:userId,relationUserId:relationUserId}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
          {field: 'funcModelId', title: 'ID'}
          ,{field: 'name', title: '功能模块名称',templet:function (record){
                  return record.funcModel.name;
              }}
          ,{field: 'type', title: '功能类型',templet:function (record){
                  return record.funcModel.typeName;
              }}
          ,{field: 'iconUrl', title: 'ICON图标',templet:function (record){
                  if(record.funcModel.iconUrl) {
                      return '<img src="' + record.funcModel.iconUrl + '" style="max-height:100%;margin:auto;display:block;">';
                  }else{
                      return '<img src="'+layui.setter.webRoot + '/layuiadmin/style/res/default01.png" style="max-height:100%;margin:auto;display:block;"/>';
                  }
              }}
          ,{field: 'stateName', title: '状态'}
          ,{field: 'createDate', title: '创建时间'}
          ,{title: '操作', width: 80, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: false
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //事件
  var active = {
      addFuncModel:function (){
          layer.open({
              type: 2
              ,title: '新增功能模块'
              ,content: layui.setter.webRoot + '/agent/relation/user/func_model/add.html?userId='+userId+'&relationUserId='+relationUserId
              ,maxmin: true
              ,area: ['500px', '450px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/agent/relation/user/func_model/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.msg('新增成功',{icon: 1});
                                  table.reload('LAY-table')
                                  layer.close(index); //关闭弹层
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,delFuncModel:function(obj){
          layer.confirm('确定要移除该功能模块吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/agent/relation/user/func_model/del.do',
                  data:{userId:userId,relationUserId:relationUserId,funcModelId:obj.data.funcModelId},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          obj.del();
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('agent_relation_func_model_list', {})
});